#include "MyCImg.h"


MyCImg::MyCImg()
{
	// Khoi tao to giay ve cua minh
	myScreen = new CImg<int>(640, 400, 1, 255);	//  Define  a  640x400  color  image with  4  bits  per  color  component
	myScreen->fill(0);		//  Set  pixel  values  to  0  (color : black)
}

MyCImg::~MyCImg()
{
	delete myScreen;
	delete display;
}

// Hien thi nhung gi cua to giay myScreen ra man hinh
void MyCImg::Display()
{
	// Gan to giay cua minh len man hinh
	display = new CImgDisplay(*myScreen, "Demo CImg library", 0);

	while (!display->is_closed())
	{
		display->wait();		// Wait for any user event occuring on the current display.
	}
}

// Ham ve duong thang
void MyCImg::DrawLine(char RGBStroke[], int strokeWidth, int x1, int y1, int x2, int y2)
{
	// Khong hieu sao cho nay khong ve theo strokeWidth ???????????????????????????????????????
	myScreen->draw_line(x1, y1, x2, y2, RGBStroke, strokeWidth);
	//Display();
}


// Ham ve Elipse
void MyCImg::DrawEllipse(char RGBStroke[], int strokeWidth, int x, int y, int width, int height)
{
	myScreen->draw_ellipse(x + width / 2, y + height / 2, width / 2, height / 2, 1.0F, RGBStroke, strokeWidth, 1);
	//Display();
}

// Ham ve hinh chu nhat
void MyCImg::DrawRectangle(char RGBStroke[], int strokeWidth, int x, int y, int width, int height)
{
	//myScreen->draw_rectangle(x, y, x+ width, y + height, RGBStroke, strokeWidth, 1);	// Ve ra cham cham HCM nay.
	
	// Cach ve duong thang (tam thoi dung cach nay)
	DrawLine(RGBStroke, strokeWidth, x, y, x, y + height);
	DrawLine(RGBStroke, strokeWidth, x, y, x + width, y);
	DrawLine(RGBStroke, strokeWidth, x, y + height, x + width, y + height);
	DrawLine(RGBStroke, strokeWidth, x + width, y, x + width, y + height);

	//Display();
}

// Ham ve Text
void MyCImg::DrawText2(char RGBStroke[], char* text, int size, int x, int y)
{
	myScreen->draw_text(x, y, text, RGBStroke, 0, 1.0F, size + 15);
}

// Ham ve mui ten
void MyCImg::DrawArrow(char RGBStroke[], int strokeWidth, int x1, int y1, int x2, int y2)
{
	myScreen->draw_arrow(x1, y1, x2, y2, RGBStroke, 1.0F, 30.0F);
}

// Ham to Elipse
void MyCImg::FillEllipse(char RGBStroke[], int x, int y, int width, int height)
{
	myScreen->draw_ellipse(x + width / 2, y + height / 2, width / 2, height / 2, 1.0F, RGBStroke);
	//Display();
}

// Ham to hinh chu nhat
void MyCImg::FillRectangle(char RGBStroke[], int x, int y, int width, int height)
{
	myScreen->draw_rectangle(x, y, x + width, y + height, RGBStroke);
	//Display();
}

// Ham to hinh chu nhat
void MyCImg::FillPolygon(char RGBStroke[], MyPoint points[], int numPoints)
{
	/*CImgList<int> arrPoints;
	for (int i = 0; i < numPoints; i++)
	{
		arrPoints.insert(CImg<int>::vector(points[i].x, points[i].y));
	}*/

	CImg<int> arrPoints(numPoints, 2, 1, 1, 0);

	for (int i = 0; i < numPoints; i++)
	{
		arrPoints(i, 0) = points[i].x;
		arrPoints(i, 1) = points[i].y;
	}

	myScreen->draw_polygon(arrPoints, RGBStroke, 1.0F);
}


// Lay ten thu vien do hoa
string MyCImg::GetName()
{
	return "CImg";
}